package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.service.IKillActivityService;
import cn.itsource.ymcc.domain.KillActivity;
import cn.itsource.ymcc.query.KillActivityQuery;
import cn.itsource.ymcc.result.JSONResult;
import cn.itsource.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/killActivity")
public class KillActivityController {

    @Autowired
    public IKillActivityService killActivityService;
    private static SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillActivity killActivity){
        if(killActivity.getId()!=null){
            killActivityService.updateById(killActivity);
        }else{
            killActivity.setCreateTime(new Date());
            killActivity.setPublishStatus(0);
            killActivity.setTimeStr(format.format(killActivity.getStartTime()));
            killActivityService.insert(killActivity);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killActivityService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killActivityService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killActivityService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillActivityQuery query){
        Page<KillActivity> page = new Page<KillActivity>(query.getPage(),query.getRows());
        page = killActivityService.selectPage(page);
        return JSONResult.success(new PageList<KillActivity>(page.getTotal(),page.getRecords()));
    }

    @RequestMapping(value = "/publish/{id}",method = RequestMethod.POST)
    public JSONResult publish(@PathVariable Long id){
        killActivityService.publish(id);
        return JSONResult.success();
    }
}
